package com.spring.shops.admin.controller.shop;

import com.spring.shops.model.entity.TzHotSearch;
import com.spring.shops.model.result.HotSearchResult;
import com.spring.shops.model.vo.PageVo;
import com.spring.shops.service.HotSearchService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin")
public class HotSearchController {
    @Resource
    private HotSearchService hotSearchService;

    /**
     * 查询数据库中所有的热销商品
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/hotSearch")
    public HotSearchResult hotSearch(@RequestBody TzHotSearch tzHotSearch) {
        int i = hotSearchService.HotSearch(tzHotSearch);
        return HotSearchResult.HotSearchSuccess();
    }

    /**
     * 用ID删除数据，是物理删除，然后是用数组整体批量删除
     *
     * @param idList
     * @return
     */
    @ResponseBody
    @DeleteMapping("/hotSearch")
    public HotSearchResult DeleteHotSearch(@RequestBody Integer[] idList) {
        int i = hotSearchService.DeleteHotSearch(idList);
        return HotSearchResult.HotSuccess();
    }

    @ResponseBody
    @GetMapping("/hotSearch/info/{id}")
    public HotSearchResult selectById(@PathVariable Long id, @RequestParam("t") long t) {
        System.out.println(id);
        TzHotSearch tzHotSearch = hotSearchService.selectById(Math.toIntExact(id));
        return HotSearchResult.infoResult(tzHotSearch);
    }

    /**
     * 修改热销数据。
     *
     * @param hotSearch
     * @return
     */
    @ResponseBody
    @PutMapping("/hotSearch")
    public HotSearchResult UpdateHotSearch(@RequestBody TzHotSearch hotSearch) {
        int i = hotSearchService.UpdateHotSearch(hotSearch);
        return HotSearchResult.HotSuccess();
    }

    @ResponseBody
    @GetMapping("/hotSearch/page")
    public HotSearchResult Page(@RequestParam("t") long t,
                                @RequestParam("current") String current,
                                @RequestParam("size") String size,
                                @RequestParam(value="title", defaultValue="") String title,
                                @RequestParam(value="content",defaultValue="") String content,
                                @RequestParam(value = "status",defaultValue = "2") int status) {
        PageVo pageVo = new PageVo();
        pageVo.setCurrent(current);
        pageVo.setSize(size);
        PageVo pageVo1 = hotSearchService.selectByPage(pageVo,title,content,status);
        return HotSearchResult.PageResult(pageVo1);
    }

}
